<template>
  <div>
    这里分配权限
    <div>
      <el-tree
        ref="perTreeRef"
        :data="list"
        :props="{label: 'name'}"
        default-expand-all
        show-checkbox
        check-strictly
        node-key="id"
      />
      <el-button @click="doPerAssign">确定</el-button>
      <el-button>取消</el-button>
    </div>
  </div>
</template>

<script>
import { getPermissionList } from '@/api/permission'
import { arrayToTree } from '@/utils'
import { getRolePermission, savePermission } from '@/api/settings'
export default {
  props: {
    id: {
      required: true,
      type: String
    }
  },
  data() {
    return {
      list: []
    }
  },
  created() {
    this.loadPermissionList()
    this.loadRolePermission()
  },
  // methods:事件处理函数
  methods: {
    async loadPermissionList() {
      const res = await getPermissionList()
      this.list = arrayToTree(res.data.data, '0')
    },
    async loadRolePermission() {
      const res = await getRolePermission(this.id)
      // 设置tree中被选中的选项
      this.$refs.perTreeRef.setCheckedKeys(res.data.data.permIds)
    },
    async doPerAssign() {
      // 获取tree中被选中的选项
      const permIds = this.$refs.perTreeRef.getCheckedKeys()
      await savePermission({ id: this.id, permIds: permIds })
      this.$emit('assignPerSuccess')
      // 清空子组件弹窗当前的选中的选项
      this.$refs.perTreeRef.setCheckedKeys([])
    }
  }
}
</script>

<style>

</style>
